<?php
/**
 * (commands) Amazon订单
 */
namespace App\Console\Commands\SpApi\Order;

use App\Models\Amazon\AmazonOrder;
use App\Models\Amazon\AmazonOrderAddressBak;
use App\Models\Amazon\AmazonOrderBak;
use App\Models\Amazon\AmazonOrderItemBak;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class OrdersToBak extends Command
{
    /**
     * The name and signature of the console command.
     * 迁移数据到Bak表
     * @var string
     */
    protected $signature = 'sp-order:orders-to-bak';

    /**
     * The console command description.
     * @var string
     */
    protected $description = 'exe func';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        try {
            $orders = AmazonOrder::query()->where('purchase_date','>=','2023-12-01 00:00:00')->get();
            $i = 0;
            $num = 2000;
            $n = 1;
            while (true){
                if($orders->isEmpty())break;
                $_orders = $orders->splice($i,$num);
                DB::connection('mysql_amazon')->beginTransaction();
                echo '---start---'.$n.'=='.$num.'========'.PHP_EOL;
                foreach($_orders as $order){
                    AmazonOrderBak::query()->updateOrCreate(
                        [
                            'amazon_order_id' => $order->amazon_order_id
                        ],
                        [
                            'purchase_date' => $order->purchase_date,
                            'marketplace_id' => $order->marketplace_id,
                            'sales_channel' => $order->sales_channel,
                            'order_status' => $order->order_status,
                            'created_at' => $order->created_at,
                            'updated_at' => $order->updated_at,
                        ]
                    );
                    foreach ($order->items as $items){
                        AmazonOrderItemBak::query()->updateOrCreate(
                            [
                                'order_item_id' => $items->order_item_id,
                            ],
                            [
                                'amazon_order_id' => $items->amazon_order_id,
                                'account_id' => $items->account_id,
                                'sku' => $items->sku,
                                'asin' => $items->asin,
                                'quantity' => $items->quantity,
                                'quantity_shipped' => $items->quantity_shipped,
                                'currency' => $items->currency,
                                'price' => $items->price,
                                'shipping_price' => $items->shipping_price,
                                'gift_wrap_price' => $items->gift_wrap_price,
                                'shipping_discount' => $items->shipping_discount,
                                'promotion_discount' => $items->promotion_discount,
                                'created_at' => $items->created_at,
                                'updated_at' => $items->updated_at,
                            ]
                        );
                    }
                    AmazonOrderAddressBak::query()->updateOrCreate(
                        [
                            'amazon_order_id' => $order->amazon_order_id,
                        ],
                        [
                            'buyer_name' => $order->address->buyer_name ?: NULL,
                            'buyer_email' => $order->address->buyer_email ?: NULL,
                            'name' => $order->address->name ?: NULL,
                            'city' => $order->address->city ?: NULL,
                            'state' => $order->address->state ?: NULL,
                            'postal_code' => $order->address->postal_code ?: NULL,
                            'country' => $order->address->country ?: NULL,
                            'phone' => $order->address->phone ?: NULL,
                            'created_at' => $order->address->created_at ?: NULL,
                            'updated_at' => $order->address->updated_at ?: NULL,
                        ]
                    );
                }
                DB::connection('mysql_amazon')->commit();
                $n++;
                echo '---end---===done==='.PHP_EOL;
            }
        } catch (\Exception $e) {
            var_dump('===error msg==='.$e->getTraceAsString());
        }
        $this->info('finish!');
    }
}
